package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserLevelInfoQueryRequest;
import com.frogs.ucenter.v1.request.UserLevelInfoSaveRequest;
import com.frogs.ucenter.v1.service.UserLevelInfoService;
import com.frogs.ucenter.v1.service.UserPointLogService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userLevelInfoController")
@RequestMapping("/" + ApiConstants.VERSION)
public class UserLevelInfoController extends BaseController3 {
	
    @Resource(name=ApiConstants.VERSION + "userLevelInfoService")
    private UserLevelInfoService userLevelInfoService;
    @Resource(name=ApiConstants.VERSION + "userPointLogService")
    private UserPointLogService userPointLogService;
    
    private static final String ROOT = "/user/level";
    private static final String USER_LEVEL_MERGE = ROOT + MERGE;
    private static final String USER_LEVEL_DETAIL = ROOT + DETAIL;
    
    @RequestMapping(USER_LEVEL_MERGE)
    public Response userLevelMerge(HttpServletRequest httpRequest){
    	UserLevelInfoSaveRequest request = new UserLevelInfoSaveRequest(); 
    	Response response;
    	try {
    		request.parse(httpRequest);
    		response = userLevelInfoService.mergeLevelInfo(request);
    		request.getUserPointLog().setRemark("保存用户信息成功!");
    		response.setTip("保存用户信息成功!");
    		request.getUserPointLog().setUpResultCode(ApiResultEnum.SUCCESS.getId());
    	} catch(Exception e) {
    		request.getUserPointLog().setUpResultCode(ApiResultEnum.FAIL.getId());
    		request.getUserPointLog().setRemark(e.getMessage());
    		response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
    	} finally {
    		userPointLogService.insert(request.getUserPointLog());
    	}
    	return response;
    }
    
    @RequestMapping({ USER_LEVEL_DETAIL})
    public Response userLevelDetail(HttpServletRequest httpRequest){
    	Request request = new UserLevelInfoQueryRequest();
    	Response response;
    	try {
    		request.parse(httpRequest);
    		response = userLevelInfoService.getLevelInfo(request);
    		response.setTip("返回用户等级信息成功!");
    	} catch(Exception e) {
    		response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
    	}
    	return response;
    }

    
}